Add strings¶
Time: O(N); Space: O(1); easy
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Example 1:
Input: num1 = “123”, num2 = “45”
Output: “168”
Notes:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
[5]:
class Solution1(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
result = []
i, j, carry = len(num1) - 1, len(num2) - 1, 0
while i >= 0 or j >= 0 or carry:
if i >= 0:
carry += ord(num1[i]) - ord('0');
i -= 1
if j >= 0:
carry += ord(num2[j]) - ord('0');
j -= 1
result.append(str(carry % 10))
carry //= 10
result.reverse()
return "".join(result)
[6]:
s = Solution1()
num1 = "123"
num2 = "45"
assert s.addStrings(num1, num2) == "168"
num1 = "678"
num2 = "322"
assert s.addStrings(num1, num2) == "1000"
[7]:
class Solution2(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
length = max(len(num1), len(num2))
num1 = num1.zfill(length)[::-1]
num2 = num2.zfill(length)[::-1]
res, plus = '', 0
for index, num in enumerate(num1):
tmp = str(int(num) + int(num2[index]) + plus)
res += tmp[-1]
if int(tmp) > 9:
plus = 1
else:
plus = 0
if plus:
res += '1'
return res[::-1]
[8]:
s = Solution2()
num1 = "123"
num2 = "45"
assert s.addStrings(num1, num2) == "168"
num1 = "678"
num2 = "322"
assert s.addStrings(num1, num2) == "1000"